package com.mall.web.control.eshop;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mall.biz.eshop.EshopBizImpl;
import com.mall.client.eshop.EshopUserInfoService;
import com.mall.common.util.PageUtil;
import com.mall.enums.EshopErrorTypeEnum;
import com.mall.pojo.EshopUserInfo;
import com.mall.pojo.User;
import com.mall.query.Page;
import com.mall.query.eshop.EshopUserQuery;
import com.mall.web.annotation.EShopUserSecurity;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.Log4jUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

/**
 * 用户电话联系，运营手动录取买卖店铺需求
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/eshop/task/")
public class EshopTaskController {

	private static Logger log = LoggerFactory.getLogger(EshopTaskController.class);
	
	@Autowired
	private EshopBizImpl eshopBizImpl;
	
	@Autowired
	private EshopUserInfoService eshopUserInfoService;
	
	/**
	 * 填写用户需求
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("eshopBackSubmit")
	@EShopUserSecurity
	public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		this.eshopBizImpl.putUserIntoMap(request, response, modelMap);
		Long eshopUserInfoId = HttpUtil.getLongValueFromRequest(request, "eshopUserInfoId");
		Boolean isModify = false;
		if (eshopUserInfoId != null && eshopUserInfoId>0) {
			EshopUserInfo eshopUserInfo = this.eshopUserInfoService.getEshopUserInfoBySellerInfoId(eshopUserInfoId);
			isModify = true;
			modelMap.put("eshopUserInfo", eshopUserInfo);
			modelMap.put("isModify", isModify);
			modelMap.put("isModifyId", eshopUserInfo.getId());
			
			
		}
		
		return "eshop/task/eshopBackSubmit";
	}
	
	@EShopUserSecurity
	@RequestMapping("doPostEshop")
	public String doPostEshop( HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		EshopUserInfo eshopUserInfo = new EshopUserInfo();
		User user  = WebUtil.getCurrentUser(request);
		Boolean isModify = false;
		Long isModifyId = HttpUtil.getLongValueFromRequest(request, "isModifyId");
		if (isModifyId!=null && isModifyId>0) {
			
			eshopUserInfo = this.eshopUserInfoService.getEshopUserInfoBySellerInfoId(isModifyId);
			if (eshopUserInfo != null) {
				isModify = true;
				
			}
		}
		Integer ruzhu = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "ruzhu", 0);
		Integer type = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "type", 0);
		String name = HttpUtil.getStringValueFromRequest(request, "name");
		String phone = HttpUtil.getStringValueFromRequest(request, "phone");
		Long qq = HttpUtil.getLongValueFromRequest(request, "qq");
		String eshopType = HttpUtil.getStringValueFromRequest(request, "eshopType");
		String remark = HttpUtil.getStringValueFromRequest(request, "remark");
		String weixin = HttpUtil.getStringValueFromRequest(request, "weixin");
		
		eshopUserInfo.setName(name);
		eshopUserInfo.setPhone(phone);
		eshopUserInfo.setQq(qq);
		eshopUserInfo.setEshopType(eshopType);
		eshopUserInfo.setRemark(remark);
		eshopUserInfo.setWangwang(weixin);//旺旺字段存用户微信号
		
		eshopUserInfo.setStatus(0);
		eshopUserInfo.setUserId(user.getId());//提交数据的用户id
		eshopUserInfo.setRuzhu(ruzhu);
		eshopUserInfo.setType(type);
		eshopUserInfo.setCommOnlyKey(HttpUtil.getCommkeyId(request, response));
		Integer result = null;
		if (isModify) {
			result = this.eshopUserInfoService.modifyEshopUserInfoBySellerInfo(eshopUserInfo);
		}else{
			result = this.eshopUserInfoService.insertEshopUserInfoBySellerInfo(eshopUserInfo);
		}
		if(result== null || result<=0){
			log.error("提交错误");
			Log4jUtil.printLog(EshopErrorTypeEnum.SELLERERROR.getDesc(), eshopUserInfo.getPhone());
		}
		
		return "redirect:/eshop/task/saleOrBuyList.htm";
	}
	@EShopUserSecurity
	@RequestMapping("saleOrBuyList")
	public String saleOrBuyList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		saleOrBuyMap(request,response,modelMap);	
		return "eshop/task/saleOrBuyList";
	}
	@RequestMapping("saleOrBuyListMore")
	public String saleOrBuyListMore(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		saleOrBuyMap(request,response,modelMap);			
		return "eshop/task/saleOrBuyList";
	}
	private ModelMap saleOrBuyMap(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		String keyWord = HttpUtil.getURLDecoderValueFromRequest(request, "keyWord")	;
		Integer type = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "type",3);
		EshopUserQuery eshopUserQuery = new EshopUserQuery();
		eshopUserQuery.setOnlyKey(onlyKey);
		eshopUserQuery.setType(type);
		eshopUserQuery.setStatus(0);//展示未联系的
		eshopUserQuery.setKeyWord(keyWord);
		eshopUserQuery.setPage(page);
		eshopUserQuery.setOrderby("gmt_modify desc");
		List<EshopUserInfo> eshopUserInfoList = this.eshopBizImpl.getEshopUserInfoListByQuery(eshopUserQuery);
		
		Integer count = this.eshopBizImpl.getEshopUserInfoCountByQuery(eshopUserQuery);
		modelMap.put("pageNo", pageNo);
		modelMap.put("pageSize", pageSize);
		modelMap.put("pageCount", PageUtil.getPageNO(count, pageSize));
		modelMap.put("eshopUserInfoList", eshopUserInfoList);
		modelMap.put("count", count);
		modelMap.put("keyWord", keyWord);
		modelMap.put("type", type);
		modelMap.put("user", user);
		return modelMap;
	}
	
	@RequestMapping
	@EShopUserSecurity
	public ResponseEntity<Boolean> operationEshopUserInfo(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "status");
		Long eshopUserInfoId = HttpUtil.getLongValueFromRequest(request, "eshopUserInfoId");
		Long operaterId = WebUtil.getCurrentUser(request).getId();
		if (status == null || eshopUserInfoId == null) {
			log.error("提交数据id为null");
			responseEntity.initErrorResponseEntity(false, "id or status is null");
			return responseEntity;
		}
		EshopUserInfo eshopUserInfo = this.eshopUserInfoService.getEshopUserInfoBySellerInfoId(eshopUserInfoId);
		if (eshopUserInfo == null) {
			responseEntity.initErrorResponseEntity(false, "id or status is null");
			return responseEntity;
		}
		eshopUserInfo.setStatus(status);
		if (operaterId !=null && operaterId>0) {
			eshopUserInfo.setOperatorId(operaterId);
		}
		Integer result = this.eshopUserInfoService.modifyEshopUserInfoBySellerInfo(eshopUserInfo);
		if (result != null && result>0) {
			responseEntity.initSuccResponseEntity(true, "operate success");
			return responseEntity;
		}
		responseEntity.initErrorResponseEntity(false, "modify is error");
		return responseEntity;
		
	}

}
